<script lang="ts" setup>
import { get, post, message, loadFile } from '@/utils'
import Editor from '@/components/Editor/index.vue'
import { FormInstance } from '@arco-design/web-vue'
const props = defineProps<{
  show: boolean
  id: number | string
  info?: any
}>()
const emit = defineEmits(['update:show', 'reset', 'confirm'])
const formRef = ref() as Ref<FormInstance>
const detail = reactive({
  brief: '',
  code: '',
  content: '',
  sort: 0,
  title: '',
})
const submit = done => {
  formRef.value.validate(err => {
    if (!err) {
      const query = Object.assign({}, detail)
      post(
        { url: '/server-base/admin/help/submit', data: query },
        ({ code, msg }) => {
          message({
            type: code == 200 ? 'success' : 'error',
            content: msg,
          })
          emit('update:show', false)
          emit('confirm')
          done(true)
        },
        () => done(false)
      )
    } else done(false)
  })
}
const getInfo = () => {
  get({ url: '/server-base/admin/help/info', data: { id: props.id } }, ({ data }) => {
    Object.assign(detail, data)
  })
}
onMounted(() => {
  if (props.id) getInfo()
})
</script>
<template>
  <CDrawer v-model:visible="props.show" @before-ok="submit" :mask-closable="false" width="700px" :title="props.id ? '编辑' : '新建'" @cancel="emit('update:show', false)">
    <div class="content">
      <a-form :model="detail" ref="formRef" :label-col-props="{ span: 3 }" :wrapper-col-props="{ span: 21 }">
        <a-form-item label="标题" field="title" :rules="[{ required: true, message: '请输入标题' }]" :validate-trigger="['change', 'blur']">
          <a-input v-model="detail.title" placeholder="请输入标题" />
        </a-form-item>
        <a-form-item label="简介" field="brief" :rules="[{ required: true, message: '请输入简介' }]" :validate-trigger="['change', 'blur']">
          <a-textarea v-model="detail.brief" placeholder="请输入简介" />
        </a-form-item>
        <div class="grid-2">
          <a-form-item label="编码" field="code" :label-col-style="{ flex: '0 0 25%' }" :wrapper-col-style="{ flex: '0 0 75%' }" :rules="[{ required: true, message: '请输入编码' }]" :validate-trigger="['change', 'blur']">
            <a-input v-model="detail.code" placeholder="请输入编码" />
          </a-form-item>
          <a-form-item label="排序" field="sort" :label-col-style="{ flex: '0 0 25%' }" :wrapper-col-style="{ flex: '0 0 75%' }" :rules="[{ required: true, message: '请输入排序' }]" :validate-trigger="['change', 'blur']">
            <a-input-number :min="0" v-model="detail.sort" placeholder="请输入排序" />
          </a-form-item>
        </div>
        <a-form-item label="内容" field="content" :rules="[{ required: true, message: '请输入内容' }]" :validate-trigger="['change', 'blur']">
          <Editor v-model:content="detail.content" />
        </a-form-item>
      </a-form>
    </div>
  </CDrawer>
</template>
<style lang="less" scoped></style>
